package com.zpz.framework.zpzoauth.common.utils;

import com.alibaba.fastjson.JSON;
import com.zpz.framework.zpzoauth.common.redis.ZpzOauthRedisService;
import com.zpz.framework.zpzoauth.dao.ZpzOauthFrameReleaseAccessMapper;
import com.zpz.framework.zpzoauth.pojo.pe.FrameReleaseAccessPe;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.util.AntPathMatcher;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;
/**
 * @author zhangpanzhi
 * @time 2019-07-09
 * @description 未经本人允许请勿随便改动，尊重劳动
 * */
@Component
public class ZpzOauthFilterReleaseAccess {
    private AntPathMatcher antPathMatcher = new AntPathMatcher();
    @Resource
    private ZpzOauthRedisService zpzOauthRedisService;
    @Resource
    private ZpzOauthFrameReleaseAccessMapper zpzOauthFrameReleaseAccessMapper;
    @Resource
    private RedisTemplate redisTemplate;
    public static String APPLICATION_NAME;
    @Value("${zpzOauth.redis.pre}")
    public void setApplicationName(String applicationName){
        APPLICATION_NAME=applicationName;
    }

    public List<FrameReleaseAccessPe> getReleaseAccess(String clientId) throws Exception{
        /************处理放行权限************/
        String key=APPLICATION_NAME+"_"+ ZpzOauthCommonConstant.RELEASE_ACCESS+":"+clientId;
        List<FrameReleaseAccessPe> res= zpzOauthRedisService.getList(key,FrameReleaseAccessPe.class);
        System.out.println(JSON.toJSONString(res));
        if (res==null){
            res = zpzOauthFrameReleaseAccessMapper.selectReleaseAccess(clientId);
            if (res.size()<=0){
                res=new ArrayList<>();
            }
            zpzOauthRedisService.setList(key, res);
            redisTemplate.expire(key, ZpzOauthCommonConstant.RELEASE_ACCESS_HOURS, TimeUnit.HOURS);
        }
        return res;
    }
    // 0三方放行1内部放行2不放行
    public int isReleaseAccess(String clientId, HttpServletRequest request) throws Exception{
        /************处理放行权限************/
        List<FrameReleaseAccessPe> res= getReleaseAccess(clientId);
        if (res!=null&&res.size()>0){
            for (FrameReleaseAccessPe ra :res) {
                if (ra.getType()==2){
                    if (StringUtils.isNotBlank(ra.getMethodUrl())&&ra.getMethodUrl().equals(request.getRequestURI())){
                        return 0;
                    }
                }else{
                    if (StringUtils.isNotBlank(ra.getClientId())){
                        if (StringUtils.isNotBlank(ra.getClientId())&&ra.getClientId().equals(clientId)){
                            Integer i = validInnerRealseAccess(ra, request);
                            if (i!=null){
                                return i;
                            }
                        }
                    }else{
                        Integer i = validInnerRealseAccess(ra, request);
                        if (i!=null){
                            return i;
                        }
                    }
                }
            }
        }
        /************************/
        return 2;
    }
    private Integer validInnerRealseAccess(FrameReleaseAccessPe ra, HttpServletRequest request){
        if (StringUtils.isNotBlank(ra.getMethodUrl())&&ra.getMethodUrl().indexOf("/**")>-1){
            String pattern = ra.getMethodUrl().substring(0, ra.getMethodUrl().lastIndexOf("/**"));
            if (request.getRequestURI().startsWith(pattern)){
                return 1;
            }
        }else{
            if (StringUtils.isNotBlank(ra.getMethodUrl())&&antPathMatcher.match(ra.getMethodUrl(), request.getRequestURI())){
                return 1;
            }
        }
        return null;
    }
}
